package com.jyt.peripheral.print

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import com.caysn.printerlibs.printerlibs_caysnpos.printerlibs_caysnpos
import com.jyt.peripheral.CCache
import com.jyt.peripheral.print.PrintManager.Companion.mContext
import com.jyt.peripheral.print.PrintManager.Companion.toast
import com.sun.jna.Pointer
import com.sun.jna.WString
import java.io.IOException

/**
 * @author 陈聪
 * date 2021/4/7:14:10
 * 内部打印
 */
class InerPrinter : BasePrinter() {

    private var h = Pointer.NULL
    private var devicePaths: Array<String>? = null
    private var strUSBPort: String? = null
    private var tryIndex = 0

    /**是否可进行打印*/
    var isCanPrint: Boolean = false
    var isPrint: Boolean = false

    /**
     * 打印机断开监听
     */
    private var closedCallback = printerlibs_caysnpos.on_port_closed_callback {
        close()
    }

    /**
     * 初始化
     */
    init {
        init()
    }

    /**
     * 开始小票打印机连接
     */
    fun openPort() {
        toast("小票打印机开始连接")
        Log.e("InerPrinter", "openPort-->$strUSBPort")
        h = printerlibs_caysnpos.INSTANCE.CaysnPos_OpenUsbVidPidStringA(strUSBPort)
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetClosedEvent(h, closedCallback, Pointer.NULL)
        //连接失败
        if (h == null) {
            if (devicePaths == null) {
                devicePaths = printerlibs_caysnpos.CaysnPos_EnumUsbVidPid_Helper.CaysnPos_EnumUsbVidPidA()
            }
            if (devicePaths != null) {
                tryIndex++
                if (devicePaths?.size!! > tryIndex) {
                    strUSBPort = devicePaths!![tryIndex]
                    openPort()
                } else {
                    toast("小票打印机连接失败，请检查")
                }
            } else {
                toast("小票打印机连接失败，请检查")
            }
        } else {
            toast("小票打印机连接成功")
            CCache.get(mContext).put("strUSBPort", strUSBPort)
            PrintManager.printCaches()
        }
    }

    override fun init() {
        tryIndex = 0
        isPrint = false
        strUSBPort = CCache.get(mContext).getAsString("strUSBPort")
        if (strUSBPort.isNullOrEmpty()) {
            devicePaths =
                printerlibs_caysnpos.CaysnPos_EnumUsbVidPid_Helper.CaysnPos_EnumUsbVidPidA()
            if (devicePaths != null && devicePaths?.isNotEmpty()!!) {
                strUSBPort = devicePaths!![0]
            }
        }
        if (!strUSBPort.isNullOrEmpty()) {
            openPort()
        }
    }

    /**
     * 打开钱箱
     */
    fun openDrawer() {
        printerlibs_caysnpos.INSTANCE.CaysnPos_KickOutDrawer(h, 0, 100, 100)
        val result = printerlibs_caysnpos.INSTANCE.CaysnPos_KickOutDrawer(h, 1, 100, 100)
        if (result == 0) toast("钱箱打开失败")
    }


    /**
     * 是否已连接打印机
     */
    override fun hadConnect(): Boolean {
        return h != null
    }

    fun reSetprinter() {
        printerlibs_caysnpos.INSTANCE.CaysnPos_ResetPrinter(h)
    }

    /**
     * 开始票据打印
     */
    override fun startPrint(info: PrintInfo?, callBack: ((Boolean) -> Unit)?): Boolean {
        if (queryPrinterStatus()) {
            val orderPrintInfo = info?.getPrintInfo<OrderPrintInfo>()
            if (orderPrintInfo == null) {
                callBack?.invoke(false)
                return false
            }
            val nLineStartPos = intArrayOf(0, 201, 401)
            val nLineEndPos = intArrayOf(200, 400, 575)
            printerlibs_caysnpos.INSTANCE.CaysnPos_ResetPrinter(h)
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetMultiByteMode(h)
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetMultiByteEncoding(
                h, printerlibs_caysnpos.MultiByteModeEncoding_UTF8
            )
            printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 2)
            // 双横线-打印一条2倍高横线
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
                h, nLineStartPos.size, nLineStartPos, nLineEndPos
            )
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
                h, nLineStartPos.size, nLineStartPos, nLineEndPos
            )
            //设置距离上部2行距离
            printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 2);

            //店铺名称-居中打印
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetAlignment(
                h, printerlibs_caysnpos.PosAlignment_HCenter
            )
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetTextBold(h, 1)//设置粗体
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                h, WString("这里是店铺名称")
            )
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetTextBold(h, 0)//撤销粗体

            //设置距离上部1行距离
            printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 1);
            //订单号：DD2104010010001--居左打印
            //日期： 2021-04-01 时间：19:21:38--居左打印
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetAlignment(
                h, printerlibs_caysnpos.PosAlignment_Left
            )
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                h,
                WString("\r\n订单号：${orderPrintInfo.orderNo}\r\n日期： ${orderPrintInfo.createTime}\r\n")
            )

            // 双横线-打印一条2倍高横线
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
                h, nLineStartPos.size, nLineStartPos, nLineEndPos
            )
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
                h, nLineStartPos.size, nLineStartPos, nLineEndPos
            )

            //设置距离上部1行距离
            printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 1);
            //标题栏 商品---单价-数量--金额
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 0)
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("商品"))
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 90)
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("单价"))
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 200)
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("数量"))
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 300)
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("金额\r\n"))
            // 单行线
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
                h, nLineStartPos.size, nLineStartPos, nLineEndPos
            )
            //设置距离上部1行距离
            printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 1);
            orderPrintInfo.goodsList?.forEach {
                //商品---单价-数量--金额
                printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 0)
                printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                    h, WString(
                        "${it.goodsName}${
                        if (!it.skuVar.isNullOrEmpty()) {
                            "（${it.skuVar}${
                            if (!it.skuValues.isNullOrEmpty()) {
                                "/${it.skuValues}"
                            } else {
                                ""
                            }
                            }）"
                        } else {
                            ""
                        }
                        }\r\n"
                    )
                )
                printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 60)
                printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                    h, WString("¥${it.unitPrice}/${it.unit}")
                )
                printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 200)
                printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                    h, WString("${it.quantity}${it.unit}")
                )
                printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 300)
                printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                    h, WString("${it.actualAmountStr}\r\n")
                )
            }

            // 双横线
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
                h, nLineStartPos.size, nLineStartPos, nLineEndPos
            )
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
                h, nLineStartPos.size, nLineStartPos, nLineEndPos
            )

            //应收金额 实收金额 礼券支付 现金支付
            printerlibs_caysnpos.INSTANCE.CaysnPos_SetAlignment(
                h, printerlibs_caysnpos.PosAlignment_Left
            )
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                h, WString(
                    "\r\n应收金额：¥${orderPrintInfo.totalAmount}" +
                            if (orderPrintInfo.discountAmount ?: 0.0 > 0.0) {
                                "\r\n优惠金额：¥${orderPrintInfo.discountAmount}"
                            } else {
                                ""
                            } +
                            if (orderPrintInfo.zeroAmount ?: 0.0 > 0.0) {
                                "\r\n抹零金额：¥${orderPrintInfo.zeroAmount}"
                            } else {
                                ""
                            } +
                            "\r\n实收金额：${orderPrintInfo.actualAmountStr}" +
                            if (orderPrintInfo.rmbAmount ?: 0.0 > 0.0) {
                                "\r\n现金支付：¥${orderPrintInfo.rmbAmount}"
                            } else {
                                ""
                            } +
                            if (orderPrintInfo.zfbAmount ?: 0.0 > 0.0) {
                                "\r\n支付宝支付：¥${orderPrintInfo.zfbAmount}"
                            } else {
                                ""
                            } +
                            if (orderPrintInfo.wxAmount ?: 0.0 > 0.0) {
                                "\r\n微信支付：¥${orderPrintInfo.wxAmount}"
                            } else {
                                ""
                            } +
                            if (orderPrintInfo.xcxAmount ?: 0.0 > 0.0) {
                                "\r\n小程序支付：¥${orderPrintInfo.xcxAmount}"
                            } else {
                                ""
                            } + "\r\n"
                )
            )
            // 为消费者提供最好的牛肉！
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                h, WString("\r\n为消费者提供最好的牛肉！\r\n")
            )
            // 欢迎关注公众号【牛大吉外送】
            printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
                h, WString("欢迎关注小程序【牛大吉外送】\r\n")
            )

            //牛大吉小程序二维码
            printBitmap("minpro_qr_icon.png")
            val result = printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 3)
            if (result == 0) toast("打印失败")
            callBack?.invoke(result != 0)
            return true
        } else {
            return false
        }
    }

    /**
     * 开始票据打印
     */
    fun testPrint(callBack: ((Boolean) -> (Unit))?) {
        //初始化打印
        val nLineStartPos = intArrayOf(0, 201, 401)
        val nLineEndPos = intArrayOf(200, 400, 575)
        printerlibs_caysnpos.INSTANCE.CaysnPos_ResetPrinter(h)
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetMultiByteMode(h)
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetMultiByteEncoding(
            h, printerlibs_caysnpos.MultiByteModeEncoding_UTF8
        )
        printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 2)
        // 双横线-打印一条2倍高横线
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
            h, nLineStartPos.size, nLineStartPos, nLineEndPos
        )
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
            h, nLineStartPos.size, nLineStartPos, nLineEndPos
        )
        //设置距离上部2行距离
        printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 2);

        //店铺名称-居中打印
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetAlignment(
            h, printerlibs_caysnpos.PosAlignment_HCenter
        )
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetTextBold(h, 1)//设置粗体
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("牛大吉凯旋城店"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetTextBold(h, 0)//撤销粗体

        //设置距离上部1行距离
        printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 1);
        //订单号：DD2104010010001--居左打印
        //日期： 2021-04-01 时间：19:21:38--居左打印
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetAlignment(
            h, printerlibs_caysnpos.PosAlignment_Left
        )
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
            h, WString("\r\n订单号：DD2104010010001\r\n日期： 2021-04-01 时间：19:21:38\r\n")
        )

        // 双横线-打印一条2倍高横线
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
            h, nLineStartPos.size, nLineStartPos, nLineEndPos
        )
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
            h, nLineStartPos.size, nLineStartPos, nLineEndPos
        )
        //设置距离上部1行距离
        printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 1);
        //标题栏 商品---单价-数量--金额
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 0)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("商品"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 130)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("单价"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 200)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("数量"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 300)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("金额\r\n"))
        // 单行线
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
            h, nLineStartPos.size, nLineStartPos, nLineEndPos
        )
        //设置距离上部1行距离
        printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 1);
        //商品---单价-数量--金额
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 0)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("牛干巴（香辣100给）\r\n"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 130)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("45.0"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 200)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("2"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 300)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("90.00\r\n"))
        //商品---单价-数量--金额
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 0)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("牛干巴\r\n"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 130)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("45.0"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 200)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("2"))
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetHorizontalAbsolutePrintPosition(h, 300)
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(h, WString("90.00\r\n"))

        // 双横线
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
            h, nLineStartPos.size, nLineStartPos, nLineEndPos
        )
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintMultipleHorizontalLinesAtOneRow(
            h, nLineStartPos.size, nLineStartPos, nLineEndPos
        )

        //应收金额 实收金额 礼券支付 现金支付
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetAlignment(
            h, printerlibs_caysnpos.PosAlignment_Left
        )
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
            h, WString("\r\n应收金额：237.80\r\n实收金额：237.80\r\n礼券支付：218.00\r\n现金支付：19.80\r\n")
        )
        // 欢迎关注公众号【牛大吉外送】
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
            h, WString("\r\n欢迎关注公众号【牛大吉外送】\r\n")
        )
        // 牛大吉总部：深圳市南山区讯美科技大厦2号楼15-7号
        printerlibs_caysnpos.INSTANCE.CaysnPos_PrintTextInUTF8W(
            h, WString("牛大吉总部：深圳市南山区讯美科技大厦2号楼15-7号\r\n")
        )
        //牛大吉小程序二维码
        printBitmap("minpro_qr_icon.png")
        val result = printerlibs_caysnpos.INSTANCE.CaysnPos_FeedLine(h, 3)
        if (result == 0) toast("打印失败")
        callBack?.invoke(result != 0)
    }

    /**
     * 打印图片
     */
    private fun printBitmap(pic: String) {
        val bitmap = getImageFromAssetsFile(mContext!!, pic) ?: return
        val width = bitmap.width
        val height = bitmap.height
        val page_width = 384
        var dstw = (width * 0.6).toInt()
        var dsth = (height * 0.6).toInt()
        if (dstw > page_width) {
            dstw = page_width
            dsth = (dstw * (height.toDouble() / width)).toInt()
        }
        printerlibs_caysnpos.INSTANCE.CaysnPos_SetAlignment(
            h, printerlibs_caysnpos.PosAlignment_HCenter
        )
        printerlibs_caysnpos.CaysnPos_PrintRasterImageWithCompress_Helper.CaysnPos_PrintRasterImageWithCompressFromBitmap(
            h, dstw, dsth, bitmap, 0, printerlibs_caysnpos.ImageCompressionMethod_None
        )
    }

    private fun getImageFromAssetsFile(
        ctx: Context, fileName: String?
    ): Bitmap? {
        var image: Bitmap? = null
        val am = ctx.resources.assets
        try {
            val `is` = am.open(fileName!!)
            image = BitmapFactory.decodeStream(`is`)
            `is`.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return image
    }

    /**
     * 断开连接
     */
    override fun close() {
        if (h !== Pointer.NULL) {
            printerlibs_caysnpos.INSTANCE.CaysnPos_Close(h)
            h = Pointer.NULL
            toast("小票打印机已断开连接")
        }
    }


    /**
     * 查看打印机状态
     */
    fun queryPrinterStatus(): Boolean {
        isCanPrint = true
        var msg = ""
        printerlibs_caysnpos.INSTANCE.CaysnPos_ClearMemoryData(h)
        printerlibs_caysnpos.INSTANCE.CaysnPos_ResetPrinter(h)
        val status = printerlibs_caysnpos.INSTANCE.CaysnPos_QueryPrinterStatus(h, 3000)
        if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_QUERYFAILED(status.toLong())) {
            msg = "查询打印机状态失败"
        } else {
//            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_DRAWER_OPENED(status.toLong())) {
//                msg = "Drawer opened"
//            }
            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_OFFLINE(status.toLong())) {
                msg = "打印机已离线"
                isCanPrint = false
            }
//            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_COVERUP(status.toLong())) msg += "Cover up\r\n"
//            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_FEED_PRESSED(status.toLong())) msg += "Feed key pressed\r\n"
            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_NOPAPER(status.toLong())) {
                msg = "打印纸已用完"
                isCanPrint = false
            }
            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_ERROR_OCCURED(status.toLong())) {
                msg = "打印错误"
                isCanPrint = false
            }
            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_CUTTER_ERROR(status.toLong())) {
                msg += "Cutter error"
                isCanPrint = false
            }
            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_UNRECOVERABLE_ERROR(
                    status.toLong()
                )
            ) {
                msg = "无法恢复的错误"
                isCanPrint = false
            }
            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_DEGREE_OR_VOLTAGE_OVERRANGE(
                    status.toLong()
                )
            ) {
                msg = "度数或电压超量程"
                isCanPrint = false
            }
            if (printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_PAPER_NEAREND(status.toLong())) {
                msg = "Paper near end"
                isCanPrint = false
            }
//            if (!printerlibs_caysnpos.PL_PRINTERSTATUS_Helper.PL_PRINTERSTATUS_PAPER_TAKEOUT(status.toLong())) {
//                msg = "Paper not takeout"
//                isCanPrint = false
//            }
        }
        toast(msg)
        return isCanPrint
    }


}